package com.ziyun.job.modules.job.task.order;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.modules.member.entity.UserPointEntity;
import com.ziyun.erp.modules.member.entity.UserWalletEntity;
import com.ziyun.erp.modules.member.service.UserPointService;
import com.ziyun.erp.modules.member.service.UserWalletService;
import com.ziyun.erp.modules.order.entity.OrderInfoEntity;
import com.ziyun.erp.modules.order.service.OrderInfoService;
import io.renren.modules.sys.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author jiangli
 * @date 2018-12-01 14:40
 */
@Component("orderPointTask")
public class OrderPointTask {
    /**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger(OrderPointTask.class);

    @Autowired
    private UserWalletService userWalletService;
    @Autowired
    private UserPointService userPointService;
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private MailService mailService;

    //积分计算
    @Transactional
    public void computePoint() {
        //查询未计算积分的订单
        logger.info("========== 开始查询未计算积分的订单 ==========");
        List<OrderInfoEntity> orderInfoEntityList = orderInfoService.selectList(new EntityWrapper<OrderInfoEntity>().gt("createtime", "2018-11-2 00:00:00").eq("status", "finish")
                .eq("pay_status", "1").ne("source", "erp").eq("promotion_type", "normal").eq("reckon_score",false));

        for (OrderInfoEntity orderInfoEntity : orderInfoEntityList) {
            //修改order_info表积分状态为已计算1
            orderInfoEntity.setReckonScore(true);
            orderInfoService.updateById(orderInfoEntity);

            //计算积分:按payed(订单已支付金额) 保留整数 普通  支付金额*0.1   大纸  支付金额*0.001
            BigDecimal payed = orderInfoEntity.getPayed();
            payed=payed.multiply(new BigDecimal("0.1"));
            Integer point = payed.intValue();
            if (point <= 0) {
                continue;
            }
            //更新用户钱包表user_wallet的积分
            UserWalletEntity userWallet = userWalletService.selectById(orderInfoEntity.getUserId());
            if (userWallet == null) {
                //未找到用户钱包表,发邮件
                mailService.sendSysErrorMail("[来自:定时任务门店订单同步到order_info表中]订单号" + orderInfoEntity.getOrderId() + "未找到用户!");
                continue;
            }
            userWalletService.updatePointById(userWallet.getUserId(),point);
            //更新积分流水表user_point
            UserPointEntity userPointEntity = new UserPointEntity();
            userPointEntity.setUserId(orderInfoEntity.getUserId());
            userPointEntity.setPoint(userWallet.getPoint()+point);
            userPointEntity.setChangePoint(point);
            userPointEntity.setAddtime(new Date());
            userPointEntity.setReason("订单"+orderInfoEntity.getOrderId()+"支付完成赠送积分");
            userPointService.insert(userPointEntity);
        }
        logger.info("========== 更新订单积分完成 ==========");
    }




}
